<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>

<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
    request.setAttribute("basePath", basePath);
%>
<%@ include file="/resources/js/easyui/taglibs.jsp" %>

<!DOCTYPE>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="format-detection" content="telephone=no"/>
    <title>用户支付</title>
    <script type="text/javascript" src="${basePath}resources/js/wechat/js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="${basePath}resources/js/wechat/wrongPoints.js"></script>
    <script type="text/javascript" src="${basePath}resources/js/wechat/js/common.js"></script>
    <script type="text/javascript" src="${basePath}resources/js/fontset.js"></script>
    <link type="text/css" rel="stylesheet" href="${basePath}resources/css/pay.css">
    <link type="text/css" rel="stylesheet" href="${basePath}resources/css/common.css">

    <style>
        .page {
            padding: 0;
        }

        .zf_length {
            width: 21rem;
            overflow: hidden;
            background: #f7f7f7;
            margin: 0 auto;
        }

        .zf_length ul li {
            width: 6rem;
            height: 5rem;
            float: left;
            margin: 0.5rem;
            overflow: hidden;
            border: 1px solid #38adff;
            box-sizing: border-box;
            border-radius: 0.3rem;
            background: #fff;
        }

        .zf_length ul li a {
            display: block;
            text-align: center;
            line-height: 5rem;
            position: relative;
            font-size: 1rem;
            color: #38adff;
        }

        .zf_length ul li a:active {
            background-color: #38adff;
            color: #fff;
        }

        .home_paypop {
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            z-index: 9999;
            background: rgba(0, 0, 0, 0.5);
            display: none;
        }

        .home_paypop_inner {
            position: absolute;
            padding: 1rem 1.5rem;
            left: 15%;
            top: 50%;
            margin-top: -5rem;
            width: 70%;
            height: 10rem;
            z-index: 2;
            background: #fff;
            border-radius: 0.3rem;
        }

        .home_paypop_inner p {
            height: 2rem;
            line-height: 2rem;
            text-align: center;
        }
    </style>
</head>
<body>
<div class="loading"></div>

<div class="page home_page zhifuPage">
    <div class="home_zhifuinner">
        <div class="zf_logo">
            <%--<p><img src="${basePath}resources/images/wechat/login_tx.png" alt=""></p>--%>
            <p><img src="${sysNotice.logourl}" alt=""></p>
        </div>
        <div class="zf_sbzt"><span>设备情况</span><i><span
                id="pay_bdonline"   ${status eq "true"?"":"style='color: #f00;'"} >${status eq "true"?"在线":"离线"}</span></i>
        </div>
        <div class="zf_yhm" ${disCode eq 1 ? "" : "style=\"display: none\""}><span>使用优惠码</span></div>
        <div class="zf_length">
            <ul>
                <c:forEach var="entry" items="${discounts}" varStatus="index">
                    <%--<li><a href="JavaScript:;"><span>${entry.money}</span>元${entry.coin}币</a></li>--%>
                    <li><a href="javascript:;"><span>${entry.discountname}</span></a></li>
                    <input type="hidden" value="${entry.discountid }" id="discountid${index.index}"/>
                </c:forEach>
            </ul>
        </div>
    </div>
    <%--<div class="tc_zhifuuser">游艺设备•智联物联</div>--%>
    <div class="tc_zhifuuser">${sysNotice.buttomcontent}</div>
</div>


<!-- 使用优惠码提示 -->
<div class="tc_youhuimapop ty_pop">
    <div class="popbg"></div>
    <div class="youhuima_inner">
        <p>使用优惠码</p>

        <p><input type="text" id="input_disCode" placeholder="请输入优惠码"></p>

        <p>
            <a href="javascript:;">取消</a>
            <a href="javascript:void(0);" onclick="payUsingDisCode($('#input_disCode').val())">确定</a>
        </p>
    </div>
</div>

<div class="home_paypop">
    <div class="home_paypop_inner">
        <p style='font-weight:bold;'>提&nbsp;&nbsp;示</p>

        <p style='margin-top:1rem;'>支付宝启动中，请稍候</p>
    </div>
</div>

</body>
</html>
<script type="text/javascript">
    $(function () {
//        $(".pay_bdtanchuang, .pay_bdoffline, .pay_bdpaypassword, .pay_bdkeyboard").hide();
        var pay_bdonline = $("#pay_bdonline").text();
        $(".zf_length ul li").each(function (n) {
            $(this).click(function () {
                if (!${status}) {
                    alert('设备离线，无法支付!');
                    return;
                }
                var signal = ${signal};
                if (signal < 15) {
                    alert('信号不好,请稍后再试!');
                    return;
                }
                prepay($("#discountid" + n).val());
            });
        });
    });


    function prepay(discountid) {
        $(".home_paypop").show();

        var payChannel = "${payChannel}";
        var url = "";
        if (payChannel == "sino") {
            url = '${basePath}/pay/sino/prepay';
        } else if (payChannel == "cmbc") {
            url = '${basePath}/pay/minsheng/prepay';
        } else {
            url = '${basePath}/pay/prepay';
        }

        $.ajax({
            url: url,
            type: "POST",
            data: {
                deviceid: '${deviceId}',
                openid: '${openid}',
                discountid: discountid
            },
            dataType: "json",
            cache: false,
            error: function (response) {
                $(".home_paypop").hide();
                if (response.status == 404) {
                    alert("404!服务器提交链接未找到!");
                } else if (response.status == 500) {
                    alert("服务器异常,请稍后重试!");
                } else if (response.status == 504 || response.status == "timeout") {
                    alert("网络异常,请稍后重试!");
                } else {
                    alert(response.status + "错误!");
                }
            },
            success: function (message) {
                if (message.message.type == "success") {
                    window.location.href = message.message.content;
                } else {
                    alert(message.message.content);
                }

                $(".home_paypop").hide();
            }
        });
    }


    $(".zf_yhm").on("click", function () {
        if ($(".tc_youhuimapop").is(":hidden")) {
            $("#input_disCode").val("").focus();
            $(".tc_youhuimapop").css("display", "block");
        }
    })

    function payUsingDisCode(disCode) {
        if (!${status}) {
            alert('设备离线，无法支付!');
            return;
        }
        var signal = ${signal};
        if (signal < 15) {
            alert('信号不好,请稍后再试!');
            return;
        }

        $.ajax({
            url: '${basePath}/pay/payWithCoupon',
            type: "POST",
            data: {
                deviceId: '${deviceId}',
                disCode: disCode
            },
            cache: false,
            success: function (message) {
                if (message == "SUCCESS") {
                    window.location.href = '${basePath}/pay/success';
                } else if (message == "USED") {
                    alert("优惠码已使用!");
                    return;
                } else if (message == "OUT") {
                    alert("优惠码已过期!");
                    return;
                } else if (message == "NOT") {
                    alert("优惠码未生效!");
                    return;
                } else if (message == "FAIL") {
                    alert("优惠码输入错误!");
                    return;
                } else {
                    window.location.href = '${basePath}/pay/fail';
                }
            }
        });
    }
</script>
